from django.shortcuts import render, redirect, HttpResponse, Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth import authenticate, login
from workapp.forms import LoginForm, RegisterForm, SearchFrom, FilterForm
from workapp.models import *
# Create your views here.


def index(request):
    context = {}
    # 取数据库中的房源数据
    houseinfo = HouseInfo.objects.all()
    if request.method == 'GET':
        form = SearchFrom
    if request.method == 'POST':
        area = request.POST
        return redirect(to='list')
    context['form'] = form
    context['houseinfo'] = houseinfo
    return render(request, 'index.html', context)

def list(request):
    context={}
    if request.method == 'GET':
        form = FilterForm
        if 'q' in request.GET and request.GET.get('q'):

            name = request.GET.get('q')
            area = Area.objects.filter(name=name)
            houseinfo_list = HouseInfo.objects.filter(area_to=area)
            context['name'] = name
        else:
            houseinfo_list = HouseInfo.objects.all()
        houseinfo_number = houseinfo_list.count()
    if request.method == 'POST':
        form = FilterForm(request.POST)
        if form.is_valid():
            if form.cleaned_data['typehouse'] :
                typehouse = form.cleaned_data['typehouse']
                p1 = HouseInfo.objects.filter(typehouse=typehouse)
            else:
                if 'q' in request.POST and request.POST.get('q'):

                    name = request.POST.get('q')
                    area = Area.objects.filter(name=name)
                    p1 = HouseInfo.objects.filter(area_to=area)
                else:
                    p1 = HouseInfo.objects.all()
            if form.cleaned_data['housetype'] :
                housetype = form.cleaned_data['housetype']
                p2 = p1.filter(housetype_in=housetype)
            else:
                p2 = p1
            min_rent = form.cleaned_data['min_rent']
            max_rent = form.cleaned_data['max_rent']
            p3 = p2.filter(rent_range=(min_rent,max_rent))
            if form.cleaned_data['installations'] :
                installations = form.cleaned_data['installations']
                installations_number = len(installations)
                def houseinstallations_filter(installations_number):
                    if installations_number==0:
                        return p3
                    return houseinfo_filter (installations_number).filter(installations_contiains=installations[installations_number-1])
                houseinfo_list = houseinstallations_filter(installations_number)
            else:
                houseinfo_list = p3
        houseinfo_number = houseinfo_list.count()
        print(typehouse)
        print(housetype)
        print(min_rent)
        print(max_rent)
        print(installations)
    page_robot = Paginator(houseinfo_list, 9)
    page_num = request.GET.get('page')
    try:
        houseinfo_list = page_robot.page(page_num)
    except EmptyPage:
        houseinfo_list = page_robot.page(page_robot.num_pages)
        # raise Http404('EmptyPage!')
    except PageNotAnInteger:
        houseinfo_list = page_robot.page(1)
    context['form'] = form
    context['houseinfo_list'] = houseinfo_list
    context['houseinfo_number'] = houseinfo_number
    return render(request, 'list.html', context)

def detail(request):
    context={}
    return render(request, 'detail.html', context)

def userinfo(request):
    context = {}
    modify = UserInfo.objects.get(email='19971525@qq.com')
    userinfo_id = modify.id
    collectall= Collect.objects.filter(user_id=userinfo_id)

    print('ineed=',  userinfo_id)
    print('ineed2=', collectall)
    context['collectall'] = collectall
    context['modify'] = modify
    return render(request, 'personcenter.html', context)


def index_login(request):
    context = {}
    if request.method == 'GET':
        form = LoginForm
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = User.objects.get(email=email)
            username = user.username
            user = authenticate(username=username, password=password)
            if user:
                login(request, user)
                return redirect(to='index')
            else:
                return HttpResponse('<h1>你输入的邮箱或密码错误</h1>')
    context['form'] = form
    return render(request, 'login.html', context)


def register(request):
    context={}
    if request.method == 'GET':
        form = RegisterForm
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = User.objects.create_user(username, email, password)
            user.save()
            u = UserInfo(email=email, username=username, password=password)
            u.save()
            return redirect(to='index_login')

    context['form'] = form
    return render(request, 'register.html', context)


def alteruser(request):
    modify = UserInfo.objects.get(email='19971525@qq.com')
    if request.method == 'GET':
        modifyform = personmodifyform
    if request.method == 'POST':
        modifyform = personmodifyform(request.POST)
        if modifyform.is_valid():
            username = modifyform.cleaned_data['username']
            password = modifyform.cleaned_data['password']
            choice_radio = request.POST.get('aaa') #取得radio的值

            modify.avatar_a = choice_radio
            modify.username = username
            modify.password = password
            modify.save()
            print('password=',password)
            return redirect(to='alteruser')


    context={}
    modifyform = personmodifyform
    context['modifyform'] = modifyform
    context['modify'] = modify
    return render(request, 'personmodify.html', context)

def appointment(request):
    context={}
    return render(request, 'appointment.html', context)
